package com.sercomm.ott.mediaplayer;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;

/**
 * Created by ott on 6/15/17.
 */

public class SystemStatus_bak {
    private final static String TAG = "cpuinfo";

    /*
     * memory loading
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public static double getMemoryLoading(Context mContext) {
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        //LogUtil.d(TAG,"totalmem "+ mi.totalMem+" availmem "+mi.availMem);
        return 100.0 * (mi.totalMem - mi.availMem) / mi.totalMem;
    }

    /*
     * cpu loading
     */
    public static String getCpuLoading() {
        String tp = "/proc/stat";
        String cLoad = "";
        String[] result;
        long allLoad = 0;
        long availLoad = 0;
        String twoInfo = "1 1";

        try {
            FileReader fr = new FileReader(tp);
            BufferedReader bf = new BufferedReader(fr);
            cLoad = bf.readLine();
            cLoad = cLoad.substring(cLoad.indexOf(" ")).trim();
            //LogUtil.d(TAG, "------" + cLoad);
            result = cLoad.split(" ");

            for (int i = 0; i < result.length; i++) {
                // LogUtil.d(TAG, "---result" + i + " = " + result[i]);
                // LogUtil.d(TAG, "----parselong" + i + " = " + Long.parseLong(result[i]));
                allLoad += Long.parseLong(result[i]);
            }
            availLoad = Long.parseLong(result[3]);
            twoInfo = String.valueOf(allLoad) + " " + String.valueOf(availLoad);
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

       // LogUtil.d(TAG, "--------" + twoInfo);
        return twoInfo;
    }
    /*
     * cpu load percent
     */
    public static double getCpuLoadPercent(String start,String end) {
        double percent = 0;
        try{
            long all = Long.parseLong(end.substring(0,end.indexOf(' ')).trim())
                    - Long.parseLong(start.substring(0,start.indexOf(' ')).trim());
            long avail = Long.parseLong(end.substring(end.indexOf(' ')).trim())
                    - Long.parseLong(start.substring(start.indexOf(' ')).trim());
            percent = 100.0*(all-avail)/all *(SystemStatus.getCpuCores()-SystemStatus.getCpuIdle())/SystemStatus.getCpuCores();
        }catch (Exception e){
            e.printStackTrace();
        }

        return percent;
    }
    /*
    public static String getCpuLoading(Context mContext){
        String tp="/proc/loadavg";
        String[] result;
        String cLoad="";
        try{
            FileReader fr = new FileReader(tp);
            BufferedReader bf = new BufferedReader(fr);
            cLoad = bf.readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        result=cLoad.split(" ");
        return result[0];
    }
    */

    /*
     * cpu temperature
     */
    public static int getCpuTemperature(Context mContext) {
        String temp = "/sys/class/thermal/thermal_zone0/temp";
        String cTemp = "";
        try {
            FileReader fr = new FileReader(temp);
            BufferedReader bf = new BufferedReader(fr);
            cTemp = bf.readLine();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //LogUtil.d(TAG,"---temperature "+Integer.parseInt(cTemp)/1000);
        return Integer.parseInt(cTemp) / 1000;
    }

    /*
     * product name
     */
    public static String getModuleName(Context mContext) {

        return Build.PRODUCT;
    }

    /*
     * device serial number
     */
    public static String getSerialNum(Context mContext) {

        return Build.SERIAL;
    }

    /*
     * ethernet link
     */
    public static boolean isEthernetOn(Context mContext) {
        ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                return true;
            }
        }
        return false;
    }
    /*
     * linkspeed
     */
    public static int getEthernetSpeed(){
        int speed = 0;        // 100  // Mbps
        final String file = "/sys/class/net/eth0/speed";
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = br.readLine();
            speed = Integer.parseInt(line.trim());
            br.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return speed;
    }
    /*
     * ethernet duplex
     */
    public static String getEthernetDuplex(){
        String duplex = "Unknown";        // Full
        final String file = "/sys/class/net/eth0/duplex";
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = br.readLine();
            duplex = line.trim();
            br.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return duplex;
    }
    /*
     * devide mac address
     */
    public static String getMacAddr(Context mContext) {
        String addrpath = "/sys/class/net/eth0/address";
        String macString = null;
        try {
            FileReader fr = new FileReader(addrpath);
            BufferedReader bf = new BufferedReader(fr);
            macString = bf.readLine();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return macString.trim();
    }

    /*
        public static int getVideoFrames(Context mContext,String pathString){

            MediaMetadataRetriever mmdr = new MediaMetadataRetriever();
            mmdr.setDataSource(pathString);
            String result = mmdr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE);
            mmdr.release();
            LogUtil.d(TAG,"------getvideoframes "+result);
            return Integer.parseInt(result);
        }
    */

    /*
     * cpu core number
     */
    public static int getCpuCores() {
        int cores = 0;       // 4
        final String file = "/sys/devices/system/cpu/";
        final String cpu = "cpu";
        try {
            File dir = new File(file);
            String[] fs = dir.list();
            for (int i = 0; i < fs.length; ++i) {
                if (fs[i].startsWith(cpu)) {
                    String tmp = fs[i].substring(cpu.length());
                    try {
                        if (tmp.equals("" + Integer.parseInt(tmp)))
                            ++cores;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //LogUtil.d(TAG,"------corenum "+cores);
        return cores;
    }

    /*
     * cpu core idle number
     */
    public static int getCpuIdle(){
        int idle = 0;
        String idlepath = "/sys/devices/system/cpu/offline";
        String sIdle = "";
        try{
            FileReader fr = new FileReader(idlepath);
            BufferedReader bf = new BufferedReader(fr);
            sIdle = bf.readLine().trim();
            String s[] = sIdle.split("-");
            if(s.length == 1){
                idle = 1;
            }else {
                idle = Integer.parseInt(s[1])-Integer.parseInt(s[0])+1;
            }
            fr.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        //LogUtil.d(TAG,"-----idle  "+idle);
        return idle;

    }
}



